<?php

/*
require "sorter_byname.php";
require "sorter_bydate.php";*/

require "imflib_gd.php";
/*require "imagemanager_fs.php";*/

class RGalleryCore
{
	public static $thumbquality = 80;
	public static $onpage = 20;
	public static $thumbsize = array('width'=>120, 'height'=>120);
	public static $albumdir;
	public static $thumbdir;
	
	private static $ilib;
	private static $iman;

	
	static function Init()
	{	
		self::$ilib = imfLib::getInstance();
		//self::$iman = ImageManager::getInstance();
	}
	
	static function SetDir($albumdir,$thumbdir)
	{
		self::$albumdir = $albumdir;
		self::$thumbdir = $thumbdir;
                //echo self::$thumbdir;
	}
	
	static function GetThumb($filename, $newwidth, $newheight)
	{
		$image['name'] = $filename;
               // print $image['name']."<br>";
		$path_info = pathinfo(self::$albumdir."/".$image['name']);
              /// vd($path_info);

		$image['filename'] = self::$albumdir."/".$image['name'];
               // print $image['filename']."<br>";
			
		list($thumb, $newwidth, $newheight) = self::$ilib->getThumb($image['filename'], $newwidth, $newheight, 
		self::$thumbdir."/".$path_info['filename']/*."_".$newwidth."x".$newheight*/,
		self::$thumbquality);
											
		if(!$thumb) die("<center> <b>Can't make thumb.</b> ".$image['filename']."</center>");
		$image['thumb'] = $thumb;
		$image['thumbwidth'] = $newwidth;
		$image['thumbheight'] = $newheight;
			
		return $image;
	}
	
	static function GetThumbs($page)
	{
		list($count, $images) = self::$iman->getImageList(self::$albumdir."/",new SortByDate(),$page,self::$onpage);
			
		for($i=0;$i<count($images);$i++)
		{
			$path_info = pathinfo(self::$albumdir."/".$images[$i]['name']);
			$images[$i]['filename'] = self::$albumdir."/".$images[$i]['name'];
                        $thumbname = self::$thumbdir."/".self::$albumdir."_".$path_info['filename']/*."_".self::$thumbsize['width']."x".self::$thumbsize['height']*/;
			
			list($thumb, $newwidth, $newheight) = self::$ilib->getThumb($images[$i]['filename'], self::$thumbsize['width'], self::$thumbsize['height'], $thumbname, self::$thumbquality);
											
			if(!$thumb) die("<center> <b>Can't make thumb.</b>".$images[$i]['filename']."</center>");
			$images[$i]['thumb'] = $thumb;
			$images[$i]['thumbwidth'] = $newwidth;
			$images[$i]['thumbheight'] = $newheight;
		}
		return array($count, $images);
	}
	
}

?>